import 'package:dio/dio.dart';
import 'package:puppy_store/service/requests.dart';

class AccountApi {
  static Future<Map> login(String account, String password) async {
    // 请求数据体
    Map data = {"account": account, "password": password};
    // 发送请求
    Response res = await PuppyRequestManager().handleRequest(
      "/login",
      "POST",
      data: data,
    );
    // 登录后的数据
    Map userInfo = res.data['result'];

    return userInfo;
  }

  // 刷新token接口
  static Future<Map> refreshToken(String account, String id) async {
    Map data = {'account': account, 'id': id};
    // 发送请求
    Response res = await PuppyRequestManager().handleRequest(
      "/login/refresh",
      "PUT",
      data: data,
    );
    //
    Map tokenInfo = res.data['result'];
    return tokenInfo;
  }

  /// 校验账号(用户名和手机号)是否唯一
  static Future<bool> registerCheck(String account) async {
    String path = '/register/check?account=$account';
    Response res = await PuppyRequestManager().handleRequest(path, "GET");

    Map result = res.data['result'];

    return result['valid'] as bool;
  }

  /// 获取短信验证码
  static Future<dynamic> getSMSVerifyCode(String mobile, String type) async {
    String path = '/code?mobile=$mobile&type=$type';
    Response res = await PuppyRequestManager().handleRequest(path, "GET");
    Map result = res.data;

    return result;
  }

  // 校验验证码
  static Future<bool> verifySMSCode(String mobile, String code) async {
    String path = '/register/code/check?mobile=$mobile&code=$code';
    Response res = await PuppyRequestManager().handleRequest(path, "GET");
    Map result = res.data['result'];
    return result['valid'] as bool;
  }

  // 注册用户
  static Future<dynamic> register(
    String account,
    String code,
    String mobile,
    String password,
  ) async {
    String path = '/register';
    Map data = {
      'account': account,
      'code': code,
      'mobile': mobile,
      'password': password,
      'type': 'app',
    };
    Response res = await PuppyRequestManager().handleRequest(
      path,
      "POST",
      data: data,
    );
    Map userInfo = res.data['result'];
    return userInfo;
  }
}
